package c.x.jy.service.utils;

import java.io.File;
import java.util.Random;

import javax.annotation.PostConstruct;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Component;

import com.baomidou.mybatisplus.core.toolkit.StringUtils;

@Component
public class Configuration {

	@Autowired
	private Environment env;
	/**
	 * 阿里云OSS相关配置
	 */
	// @Value("${aliyun.useOss:true}")
	public Boolean useOss;

	// @Value("${aliyun.oss.endpoint}")
	public static String OssEndpoint;
	// @Value("${aliyun.oss.accessKeyId}")
	public static String OssAccessKeyId;
	// @Value("${aliyun.oss.accessKeySecret}")
	public static String OssAccessKeySecret;
	// @Value("${aliyun.oss.bucketName}")
	public static String OssBucketName;
	// @Value("${aliyun.oss.fileUrl}")
	public static String OssFileUrl;
	// @Value("${aliyun.uploadStaticPath}")
	public static String STATIC_SERVER_URL;

	@PostConstruct
	public void init() {
		useOss = (StringUtils.isNotEmpty(env.getProperty("aliyun.useOss"))
				&& env.getProperty("aliyun.useOss").equals("true")) ? true : false;
		;

		OssEndpoint = env.getProperty("aliyun.oss.endpoint");
		OssAccessKeyId = env.getProperty("aliyun.oss.accessKeyId");
		OssAccessKeySecret = env.getProperty("aliyun.oss.accessKeySecret");
		OssBucketName = env.getProperty("aliyun.oss.bucketName");
		OssFileUrl = env.getProperty("aliyun.oss.fileUrl");
		STATIC_SERVER_URL = env.getProperty("aliyun.uploadStaticPath");
	}

	/**
	 * 导入文件上传路径
	 */
	public static String uploadPath = "";

	/**
	 * 导出文件路径
	 */
	public static String exportPath = "";

	public static final String aesKey = "jyAESkYzsg$1"; // AES加密key

	public String getfileName(String name) {
		Random random = new Random();

		StringBuffer newFileName = new StringBuffer();

		newFileName.append(System.currentTimeMillis() + "_" + random.nextInt(10000)).append(getFileExt(name));

		return newFileName.toString();
	}

	public static String getFileExt(String fileName) {
		return fileName.substring(fileName.lastIndexOf('.'), fileName.length());
	}

	private String getUploadFilePath(String uploadPath2) {
		// 获取webRoot目录
		String webRoot = System.getProperty("user.dir");
		// 用户设置的默认上传目录
		String saveDir = uploadPath;
		StringBuilder path = null;
		File[] roots = File.listRoots();
		if (StringUtils.isNotEmpty(saveDir) && (saveDir.startsWith("/") || startWithRoots(saveDir, roots))) {
			path = new StringBuilder(saveDir).append(File.separator);
		} else {
			path = new StringBuilder(webRoot).append(File.separator)
					.append(!StringUtils.isNotEmpty(saveDir) ? "upload" : saveDir).append(File.separator);
		}
		return path.toString();
	}

	private static boolean startWithRoots(String dir, File[] roots) {
		for (int i = 0; i < roots.length; i++) {
			File file = roots[i];
			if (dir.startsWith(file.getPath())) {
				return true;
			}
		}
		return false;
	}

	public static String getUploadPath() {
		return uploadPath;
	}

	public static void setUploadPath(String uploadPath) {
		if (Configuration.uploadPath == null || Configuration.uploadPath.equals("")) {
			Configuration.uploadPath = uploadPath;
		}
	}

	public static String getExportPath() {
		return exportPath;
	}

	public static void setExportPath(String exportPath) {
		if (Configuration.exportPath == null || Configuration.exportPath.equals("")) {
			Configuration.exportPath = exportPath;
		}
	}

}
